package com.mdc.mansion.api.web.modules.dishes;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesComboService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishesCombo;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishesComboItem;
import com.mdc.mansion.dataaccess.modules.dishes.entity.mo.RestaurantDishesComboMO;

@Controller
@RequestMapping(value = "${adminPath}/dishesCombo")
public class RestaurantDishesComboController extends BaseController {

    @Autowired
    private RestaurantDishesComboService restaurantDishesComboService;
    @RequestMapping("/list")
    public Result list(){
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        JSONArray jsonArray = new JSONArray();
        List<RestaurantDishesComboMO> dishesComboWithItemList = restaurantDishesComboService.findListWithItem(ContextHolder.currentRestaurantId());
        if(dishesComboWithItemList!=null&&dishesComboWithItemList.size()>0){
            // 只获取需要的字段
            for(RestaurantDishesComboMO restaurantDishesComboMO : dishesComboWithItemList){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", restaurantDishesComboMO.getId());
                jsonObject.put("name", restaurantDishesComboMO.getName());
                jsonObject.put("englishName", restaurantDishesComboMO.getEnglishName());
                jsonObject.put("chineseName", restaurantDishesComboMO.getChineseName());
                jsonObject.put("sortNum", restaurantDishesComboMO.getSortNum());
                jsonObject.put("price", restaurantDishesComboMO.getPrice());
                jsonObject.put("discountPrice", restaurantDishesComboMO.getDiscountPrice());
                List<RestaurantDishesComboItem> dishesComboItemList = restaurantDishesComboMO.getDishesComboItemList(); 
                JSONArray itemArray = new JSONArray();
                if(dishesComboItemList!=null&&dishesComboItemList.size()>0){
                    for(RestaurantDishesComboItem restaurantDishesComboItem : dishesComboItemList){
                        JSONObject itemJsonObject = new JSONObject();
                        itemJsonObject.put("id", restaurantDishesComboItem.getId());
                        itemJsonObject.put("restaurantDishesId", restaurantDishesComboItem.getRestaurantDishesId());
                        itemJsonObject.put("restaurantDishesStandardId", restaurantDishesComboItem.getRestaurantDishesStandardId());
                        itemJsonObject.put("dishesNum", restaurantDishesComboItem.getDishesNum());
                        itemJsonObject.put("switchDishes", restaurantDishesComboItem.getSwitchDishes());
                        itemArray.add(itemJsonObject);
                    }
                }   
                jsonObject.put("dishesComboItemList", itemArray);
                jsonArray.add(jsonObject);
            }
        }
        result.setProperty("dishesComboList", jsonArray);
        return result;
    }
    
    @RequestMapping("/save")
    public Result save(String data){
        Result result = new Result(ResultCode.COMMON_SUCCESS); 
        RestaurantDishesComboMO restaurantDishesComboMO = JSONObject.parseObject(data, RestaurantDishesComboMO.class);
        restaurantDishesComboService.saveDishesCombo(restaurantDishesComboMO);
        return result;
    }
    
    @RequestMapping("/delete")
    public Result delete(String data){
        Result result = new Result(ResultCode.COMMON_SUCCESS); 
        JSONObject obj = JSONObject.parseObject(data);
        String id = obj.getString("id");
        restaurantDishesComboService.delete(id);
        return result;
    }
    @RequestMapping("/sort")
    public Result sort(String data){
        Result result = new Result(ResultCode.COMMON_SUCCESS); 
        JSONObject paramObj = JSONObject.parseObject(data);
        JSONArray dishesComboListArray = paramObj.getJSONArray("dishesComboList");
        for(int i = 0,j = dishesComboListArray.size();i<j;i++){
            JSONObject obj = dishesComboListArray.getJSONObject(i);
            String id = obj.getString("id");
            Integer sortNum = obj.getInteger("sortNum");
            RestaurantDishesCombo paramRestaurantDishesCombo = new RestaurantDishesCombo();
            paramRestaurantDishesCombo.setId(id);
            paramRestaurantDishesCombo.setSortNum(sortNum);
//            RestaurantDishesCombo oldRestaurantDishesCombo =  restaurantDishesComboService.get(paramRestaurantDishesCombo);
//            if(oldRestaurantDishesCombo == null){
//                throw new BusinessException(I18nUtil.getMessage("dishes.combo.not.exists"));
//            }
            restaurantDishesComboService.updateSelective(paramRestaurantDishesCombo);
        }
        return result;
    }

}
